﻿using BPMN.Application.Contracts.DTO.Request.Unit;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Routing;
using BPMN.Domain.Entity.Unit;
using BPMN.Domain.Interface;
using BPMN.DomianShare;
using System.Collections.Generic;
using System.Reflection;

namespace BPMN.Application.Service
{
    public class UnitService : AppServiceBase<Units>, IUnitService
    {
        private readonly IRepositoryBase<Units> unit;

        public UnitService(IRepositoryBase<Units> unit) : base(unit)
        {
            this.unit = unit;
        }

        /// <summary>
        /// 添加单位信息
        /// </summary>
        /// <param name="dto">单位信息</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddUnit(Units dto)
        {
            var slist = unit.GetAll().Result.Where(x => x.Name == dto.Name && x.IsDel == false).FirstOrDefault();
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "单位名称已存在");
            }

            dto.CreateDate = DateTime.Now;
            dto.UpdateDate = DateTime.Now;
            int n = await unit.Add(dto);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        /// <summary>
        /// 删除单位信息
        /// </summary>
        /// <param name="id">单位Id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> DeleleUnit(int id)
        {
            var obj = unit.Get(id).Result;
            obj.IsDel = true;
            int n = unit.Update(obj).Result;
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "删除失败");
            }
        }

        /// <summary>
        /// 分页、显示、查询单位信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="name">单位名称</param>
        /// <param name="notes">单位备注</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<UnitDto>> GetUnit(int pageIndex, int pageSize, string name, string notes)
        {
            var obj = unit.GetAll().Result.Where(a => a.IsDel == false);
            if (!string.IsNullOrEmpty(name))
            {
                obj = obj.Where(x => x.Name.Contains(name));
            }
            if (!string.IsNullOrEmpty(notes))
            {
                obj = obj.Where(x => x.Notes.Contains(notes));
            }

            int total = obj.Count();
            int page = (int)Math.Ceiling((double)total / pageSize);
            obj = obj.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            UnitDto dto = new UnitDto();
            dto.TotalCount = total;
            dto.PageCount = page;
            dto.List = obj.ToList();

            return new ResultResponseDto<UnitDto>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = dto
            };
        }

        /// <summary>
        /// 显示单位信息
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Units>>> GetUnits()
        {
            var obj = unit.GetAll().Result.Where(a => a.IsDel == false);
            return new ResultResponseDto<List<Units>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = obj.ToList()
            };
        }

        /// <summary>
        /// 反填单位信息
        /// </summary>
        /// <param name="id">单位Id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<Units>> GetUnitById(int id)
        {
            var obj = unit.Get(id).Result;
            return new ResultResponseDto<Units>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = obj
            };
        }

        /// <summary>
        /// 修改单位信息
        /// </summary>
        /// <param name="dto">单位信息</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateUnit(Units dto)
        {
            dto.UpdateDate = DateTime.Now;
            int n = await unit.Update(dto);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "修改失败");
            }
        }
    }
}
